package com.feifei.controller;

import com.feifei.bean.City;
import com.feifei.bean.Product;
import com.feifei.bean.Type;
import com.feifei.bean.User;
import com.feifei.service.*;
import com.feifei.vo.CommentVO;
import com.feifei.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@Controller
public class ProductController {
    @Autowired
    ProductService productService;
    @Autowired
    UserService userService;
    @Autowired
    TypeService typeService;
    @Autowired
    CityService cityService;
    @Autowired
    CommentService commentService;



    @ResponseBody
    @RequestMapping("/product/getAllProducts")
    public Result getAllProducts() {
        Result result = new Result();
        List<Product> list = productService.getAllProducts();
        if (list != null && list.size() != 0) {
            result.setStatus(true);
            result.setData(list);
            result.setMsg("查询成功");
        } else {
            result.setStatus(false);
            result.setData(null);
            result.setMsg("无数据");
        }
        return result;
    }

    @ResponseBody
    @RequestMapping("/product/getProductDetails")
    public Result getProductDetail(Integer id) {

        Result result = new Result();
        //查询产品信息
        Product product = productService.getProductById(id);
        if (product == null) {
            result.setStatus(false);
            result.setData(null);
            result.setMsg("没有查询到相关数据");
            return result;
        }
        //通过众筹产品 查询发起人信息、众筹类型、城市信息
        User user=userService.getUserById(product.getUserId());
        Type type= typeService.getTypeById(product.getTypeId());
        City city=cityService.getCityById(product.getCityId());
        //获取当前产品的评论
        List<CommentVO> commentVOList=commentService.getCommentsByProductId(id);
        commentVOList.forEach(System.out::println);
        Map<String, Object> map = new HashMap<>();
        map.put("product", product);
        map.put("user", user);
        map.put("type", type);
        map.put("city", city);
        map.put("commentList", commentVOList);
        result.setStatus(true);
        result.setData(map);
        result.setMsg("查询成功");
        return result;
    }

    //添加项目或者编辑项目 下一个添加回报或者编辑回报都一样
    @ResponseBody
    @RequestMapping("/product/addProduct")
    public Result insertProduct(@RequestBody Product product){
        Result result=new Result();
        System.out.println("product = " + product);
        if(product.getId()==null&&product.getId()==0){
            product.setStatus(0);
            int id = productService.addProduct(product);
            result.setStatus(true);
            result.setData(id);
            result.setMsg("查询的id");
        }else{
            //执行编辑操作
            int i=productService.editProduct(product);
            if(i>0){
                result.setStatus(true);
                result.setData(product.getId());
                result.setMsg("编辑成功");
            }else{
                result.setStatus(false);
                result.setData(product.getId());
                result.setMsg("编辑失败");
            }
        }



        return result;
    }
    @ResponseBody
    @RequestMapping("/product/getProductByTypeId")
    public Result getProductByTypeId(@RequestParam("typeId") Integer typeId){
        System.out.println("typeId = " + typeId);
        List<Product> list=productService.getProductByTypeId(typeId);
        Result result=new Result();
        if (list != null && list.size() != 0) {
            result.setStatus(true);
            result.setData(list);
            result.setMsg("查询成功");
        } else {
            result.setStatus(false);
            result.setData(null);
            result.setMsg("无数据");
        }
        return result;
    }
    //根据项目状态筛选项目
    @ResponseBody
    @RequestMapping("/product/getProductByStatus")
    public Result getProductByStatus(@RequestParam("status") Integer status){
        List<Product> list=productService.getProductByStatus(status);
        Result result=new Result();
        if (list != null && list.size() != 0) {
            result.setStatus(true);
            result.setData(list);
            result.setMsg("查询成功");
        } else {
            result.setStatus(false);
            result.setData(null);
            result.setMsg("没有该数据");
        }
        return result;
    }


    //以下是个人中心的功能
    //    发起的项目
    @ResponseBody
    @RequestMapping("/product/getMyFaQiProduct")
    public Result getMyProduct(Integer id) {
        Result result = new Result();
        List<Product> list = productService.getMyProduct(id);
        if (list != null && list.size() != 0) {
            result.setStatus(true);
            result.setData(list);
            result.setMsg("查询我发起的项目成功");
        } else {
            result.setStatus(false);
            result.setData(null);
            result.setMsg("没有查询到我发起的项目");
        }
        return result;
    }
    //删除个人项目
    @ResponseBody
    @RequestMapping("/product/deleteMyProduct")
    public Result deleteMyProduct(Integer id) {
        Result result = new Result();
        int i = productService.deleteMyProduct(id);
        if (i>0) {
            result.setStatus(true);
            result.setMsg("删除个人项目成功");
        } else {
            result.setStatus(false);
            result.setData(null);
            result.setMsg("删除失败");
        }
        return result;
    }
}
